package edu.uni.dormitory.service.impl;

import edu.uni.dormitory.bean.StudentBed;
import edu.uni.dormitory.globalbean.*;
import edu.uni.dormitory.service.StudentBedExcelService;
import edu.uni.dormitory.service.StudentBedService;
import edu.uni.dormitory.utils.ExcelUtil;
import edu.uni.userBaseInfo1.bean.Student;
import edu.uni.userBaseInfo1.service.StudentService;
//import org.apache.commons.io.FileUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;
import java.util.*;

/**
 * @author :kuexun
 * @create : 2019/5/28 9:03
 */
@Service
public class StudentBedExcelServiceImpl implements StudentBedExcelService{
    @Autowired
    StudentService studentService;
    @Autowired
    StudentBedService studentBedService;

    @Override
    @Transactional
    public int uploadNewStudentBed(String savePath, MultipartFile file,Long loginId) throws IOException {
        //先保存文件到本地
        saveFile(savePath,file);
        int i = 0;
        //解析excel
        List<StudentBedExcelModel> data = ExcelUtil.readExcel(file.getInputStream(), StudentBedExcelModel.class);
        // 插入数据库
        for(StudentBedExcelModel d: data){
            //根据学号获得学生id
            Student student = studentService.selectByStuNo(d.getStudentNo());
            if(student == null) throw new RuntimeException("没有"+d.getStudentNo()+"这个学生");
            Long studentId = student.getId();
            //根据宿舍区、宿舍号、宿舍床位获得床位id
            Long liveBedId = getLiveBedId(d.getLiveAreaName(),d.getLiveRoom(),d.getBedNumber());
            if(liveBedId == null) throw new RuntimeException("没有"+d.getLiveAreaName()+d.getLiveRoom()+d.getBedNumber()+"床位");
            //新增入住信息
            StudentBed studentBed = new StudentBed();
            studentBed.setLiveBedId(liveBedId);
            studentBed.setLiveDate(d.getLiveDate());
            studentBed.setDeleted((long)0);
            studentBed.setByWho(loginId);
            studentBed.setDatetime(new Date());
            studentBed.setStatus((byte)1);
            studentBed.setStudentId(studentId);
            if(studentBedService.insert(studentBed)){
                i++;
            }
        }
        return i;
    }

    private Long getLiveBedId(String liveAreaName, String liveRoom, String bedNumber) {
        StudentBedClassVo vo = new StudentBedClassVo();
        vo.setAreaName(liveAreaName);
        vo.setRoom(Integer.valueOf(liveRoom));
        vo.setNumber(bedNumber);
        List<StudentBedClassVo> studentBedClassVos = studentBedService.selective(vo);
        if(studentBedClassVos != null && studentBedClassVos.size() > 0){
            return studentBedClassVos.get(0).getLiveBedId();
        }
        return null;
    }

    @Override
    public File exportStudentBedByArea(Long areaId, String path, String fileName) {
        List<StudentBedVo> studentBedVoList = studentBedService.selectStudentBedByAid(areaId);
        List<StudentBedExcelDetailModel> modelList = studentBedVo2Model(studentBedVoList);
        if(modelList != null && modelList.size() > 0){
            File file = new File(path,fileName);
            ExcelUtil.writeExcel(file,modelList);
            return file;
        }
        return null;
    }

    @Override
    public File exportStudentBedByRoom(Long roomId, String path, String fileName) {
        //根据宿舍id获取床位
        List<StudentBedVo> studentBedVoList = getStudentBedByRoomId(roomId);
        List<StudentBedExcelDetailModel> modelList = studentBedVo2Model(studentBedVoList);
        if(modelList != null && modelList.size() > 0){
            File file = new File(path,fileName);
            ExcelUtil.writeExcel(file,modelList);
            return file;
        }
        return null;
    }

    private List<StudentBedVo> getStudentBedByRoomId(Long roomId) {
        List<StudentBedVo> list=studentBedService.selectStudentBedAll();
        for(StudentBedVo studentBedVo:list){
            if(studentBedVo.getUserSex()==0){
                studentBedVo.setSex("女");
            }else
                studentBedVo.setSex("男");
            if(studentBedVo.getStatus()==0){
                studentBedVo.setState("待审");
            }else if(studentBedVo.getStatus()==1){
                studentBedVo.setState("同意");
            }else if(studentBedVo.getStatus()==-1){
                studentBedVo.setState("不同意");
            }
        }
        List<StudentBedVo> list1=new ArrayList<>();
        for(StudentBedVo studentBedVo:list){
            if (studentBedVo.getLiveRoomId().equals(roomId)){
                list1.add(studentBedVo);
                System.out.println(studentBedVo.getSex());
            }
        }
        return list1;
    }

    @Override
    public File exportStudentBedByClass(Long classId, String path, String fileName) {
        //根据班级id获取床位
        List<StudentBedClassVo> studentBedClassVos = studentBedService.selectCid(classId);
        List<StudentBedClassExcelDetailModel> modelList = studentBedClassVo2Model(studentBedClassVos);
        if(modelList != null && modelList.size() > 0){
            File file = new File(path,fileName);
            ExcelUtil.writeExcel(file,modelList);
            return file;
        }
        return null;
    }

    private List<StudentBedClassExcelDetailModel> studentBedClassVo2Model(List<StudentBedClassVo> studentBedClassVoList) {
        if(studentBedClassVoList == null || studentBedClassVoList.size() < 1) return null;
        List<StudentBedClassExcelDetailModel> models = new ArrayList<>();
        for(StudentBedClassVo vo:studentBedClassVoList){
            StudentBedClassExcelDetailModel model = new StudentBedClassExcelDetailModel();
            model.setId(vo.getId());
            model.setAreaName(vo.getAreaName());
            model.setLeaveDate(vo.getLeaveDate());
            model.setLiveDate(vo.getLiveDate());
            model.setNumber(vo.getNumber());
            model.setRoom(vo.getRoom());
            model.setSex(vo.getSex());
            model.setDepartmentName(vo.getDepartmentName());
            model.setClassName(vo.getClassName());
            model.setStuNo(vo.getStuNo());
            model.setUserName(vo.getUserName());
            models.add(model);
        }
        return models;
    }

    private List<StudentBedExcelDetailModel> studentBedVo2Model(List<StudentBedVo> studentBedVoList) {
        if(studentBedVoList == null || studentBedVoList.size() < 1) return null;
        List<StudentBedExcelDetailModel> models = new ArrayList<>();
        for(StudentBedVo vo:studentBedVoList){
            StudentBedExcelDetailModel model = new StudentBedExcelDetailModel();
            model.setId(vo.getId());
            model.setAreaName(vo.getAreaName());
            model.setLeaveDate(vo.getLeaveDate());
            model.setLiveDate(vo.getLiveDate());
            model.setNumber(vo.getNumber());
            model.setRoom(vo.getRoom());
            model.setSex(vo.getSex());
            model.setStuNo(vo.getStuNo());
            model.setUserName(vo.getUserName());
            models.add(model);
        }
        return models;
    }


    private void saveFile(String path, MultipartFile file) throws IOException {
        System.out.println(path);
        final String p = "excel";
        File dir = new File(path,p);
        if(!dir.exists()){
            dir.mkdir();
        }
        String imgName = UUID.randomUUID().toString()+file.getOriginalFilename();
        File upload = new File(dir,imgName);

//        FileUtils.copyInputStreamToFile(file.getInputStream(),upload);
    }


}
